package com.kye.express.business.president_online;

import android.arch.lifecycle.Lifecycle;
import android.content.Context;
import android.text.TextUtils;

import com.kye.express.R;
import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.RxView;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.UserSession;
import com.kye.model.base.RetrofitManager;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.president_online.PresidentOnlineResBean;
import com.kye.model.bean.president_online.PresidentOnlineScoreBean;
import com.kye.model.bean.president_online.RequestPresidentOnlineResBean;
import com.kye.model.bean.president_online.RequestPresidentOnlineScoreBean;
import com.kye.model.bean.user.User;
import com.kye.model.service.PresidentOnlineService;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public interface PresidentOnlineDetailFragmentContract {

    interface PresidentOnlineDetailView extends RxView {

        void onCompleteRes();

        void onLoadingRes();

        void onLoadedRes(PresidentOnlineResBean presidentOnlineResBean);

        void onErrorRes(String msg);

        void onCompleteScore();

        void onLoadingScore();

        void onLoadedScore(PresidentOnlineScoreBean presidentOnlineScoreBean);

        void onErrorScore(String msg);

        void onUser(User user, int type);

    }

    class PresidentOnlineDetailViewPresenter extends BasePresenter<PresidentOnlineDetailFragmentContract.PresidentOnlineDetailView> {

        private Context mContext;
        public final int TYPE_RES_IMAGE = 1;//图片
        public final int TYPE_RES_VOICE = 2;//语音留言
        public final int TYPE_RES_VOICE_REPLAY = 3;//回复语音
        public final int TYPE_RES_VOICE_FORWARD = 4;//转发语音
        public final int TYPE_SCORE = 5;//评分

        public PresidentOnlineDetailViewPresenter(Context context) {
            this.mContext = context;
        }

        public void onUser(int type) {
            UserSession.getInstance()
                    .getUserForRx()
                    .compose(getView().getProvider().<User>bindUntilEvent(Lifecycle.Event.ON_DESTROY))
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<User>() {
                        @Override
                        public void accept(User user) throws Exception {
                            getView().onUser(user, type);
                        }
                    });
        }

        /**
         * 请求服务器数据
         */
        public void loadResDatas(String guid, String type) {
            String phone = "";
            User user = UserSession.getInstance().getUser();
            if (user != null) {
                phone = user.getPhone();
            }

            RetrofitManager.getInstance().createOpenApiService(PresidentOnlineService.class)
                    .getResDatas(new RequestPresidentOnlineResBean(phone, guid, type))
                    .compose(getView().<OpenApiResult<PresidentOnlineResBean>>bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<PresidentOnlineResBean>() {

                        @Override
                        public void onLoading(Disposable d) {
                            //getView().onLoading();
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onErrorRes(mContext.getString(R.string.common_net_error));
                        }

                        @Override
                        public void onLoaded() {
                            if (getView() != null){
                                getView().onCompleteRes();
                            }
                        }

                        @Override
                        public void onSuccess(OpenApiResult<PresidentOnlineResBean> result, PresidentOnlineResBean datas) {
                            if (datas != null) {
                                getView().onLoadedRes(datas);
                            } else {
                                getView().onErrorRes(mContext.getString(R.string.common_no_more_datas));
                            }
                        }

                        @Override
                        public void onApiError(OpenApiResult<PresidentOnlineResBean> result, int code, String msg) {
                            if (!TextUtils.isEmpty(msg)) {
                                getView().onErrorRes(msg);
                            } else {
                                getView().onErrorRes(mContext.getString(R.string.common_net_error));
                            }
                        }
                    });
        }


        /**
         * 请求服务器数据
         */
        public void userScore(String code, String satisfaction, String message) {
            String phone = "";
            User user = UserSession.getInstance().getUser();
            if (user != null) {
                phone = user.getPhone();
            }

            RetrofitManager.getInstance().createOpenApiService(PresidentOnlineService.class)
                    .userScore(new RequestPresidentOnlineScoreBean(code, satisfaction, message, phone))
                    .compose(getView().<OpenApiResult<PresidentOnlineScoreBean>>bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<PresidentOnlineScoreBean>() {

                        @Override
                        public void onLoading(Disposable d) {
//                            getView().onLoading();
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onErrorScore(mContext.getString(R.string.common_net_error));
                        }

                        @Override
                        public void onLoaded() {
                            if (getView() != null){
                                getView().onCompleteScore();
                            }
                        }

                        @Override
                        public void onSuccess(OpenApiResult<PresidentOnlineScoreBean> result, PresidentOnlineScoreBean datas) {
                            getView().onLoadedScore(datas);
                        }

                        @Override
                        public void onApiError(OpenApiResult<PresidentOnlineScoreBean> result, int code, String msg) {
                            if (!TextUtils.isEmpty(msg)) {
                                getView().onErrorScore(msg);
                            } else {
                                getView().onErrorScore(mContext.getString(R.string.common_net_error));
                            }
                        }

                    });
        }
    }
}
